#include<iostream>
#include<ctime>
#include<cstdio>
#include<cstring>

#include"week02.h"


void int_sorting(void){
    std::cout << "Current function: " << __func__ << std::endl;
    int max_size,current_size;
    max_size = 1;
    current_size = 0;
    int *num = new int[max_size];
    int *tempnum;
    int temp,i,j;
    char quit;
    
    while(1){
        std::cout << "input a num, or input other char to end. " << __func__ << std::endl;
        std::cin >> temp;
        while (std::cin.fail()) { //返回cin的状态
            std::cout << "输入非数字,结束输入" << std::endl;
	        std::cin.clear();     //清除cin的错误状态
            std::cin.ignore();    //忽略缓冲区的内容，直到EOF
            quit = 1;
            break;
        }
        if(quit == 1){
            break;
        }
        num[current_size] = temp;
        current_size++;
        if(current_size > (max_size/2)){
            max_size *= 2;
            tempnum = num;
            num = new int [max_size];
            for(i=0;i<current_size;i++){
                num[i]=tempnum[i];
            }
            delete[] tempnum;
        }
        
    }
    std::cout<<"输入顺序如下："<<std::endl;
    for(i=0;i<current_size;i++){
        std::cout<<num[i]<<" ";
    }
    std::cout<<std::endl;
    //bubblesort
    std::cout<<"-----------"<<std::endl;
    for(i=(current_size-1);i>0;i--){
        for(j=0;j < i ; j++){
            if(num[j]<num[j+1]){
                temp = num[j];
                num[j] = num[j+1];
                num[j+1] = temp;
            }
        }
    }
    std::cout<<"排序结果："<<std::endl;
    for(i=0;i<current_size;i++){
        std::cout<<num[i]<<" ";
    }
    std::cout<<std::endl;
}

void Arr_Convertion_2D(void){
    std::cout << "Current function: " << __func__ << std::endl;
    int matrix1[3][3];
    int matrix2[3][3];
    int i,j,temp;
    std::cout << "input 9 nums" << std::endl;
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
        std::cin >> temp;
        matrix1[j][i] = temp;
            matrix2[i][j]=matrix1[j][i];
        }
    }
    std::cout << "原矩阵 " << std::endl;
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            std::cout << matrix1[i][j] << " ";
        }
        std::cout << std::endl;
    }
    std::cout << "转置阵 " << std::endl;
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            std::cout << matrix2[i][j] << " ";
        }
        std::cout << std::endl;
    }
    std::cout << "对角线相加=" <<(matrix1[0][0]+matrix1[1][1]+matrix1[2][2])
    <<"&"<<(matrix1[0][2]+matrix1[1][1]+matrix1[2][0])<< std::endl;
}

void FindMax_Ptr(void){
    std::cout << "Current function: " << __func__ << std::endl;
    int temp,quit,i,max,count = 0;
    int num[100];
    int* p = num;
    while(1){
        std::cout << "input a num, or input other char to end. " << __func__ << std::endl;
        std::cin >> temp;
        
        while (std::cin.fail()) { //返回cin的状态
            std::cout << "输入非数字,结束输入" << std::endl;
	        std::cin.clear();     //清除cin的错误状态
            std::cin.ignore();    //忽略缓冲区的内容，直到EOF
            quit = 1;
            break;
        }
        if(quit == 1){
            break;
        }
        num[count] = temp;
        count++;
    }
    max = num[0];
    for(i=0;i<count;i++){
        if(max < *p){
            max = *p;
        }
        p++;
    }
    std::cout << "max num =" << max << std::endl;
}

void average(int arr[], int n) {
    int sum = 0;
    double avg = 0.0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
        avg = (double)sum / n;
    std::cout << "The average of the array is " << avg << std::endl;
}

void median(int arr[], int n) {
    int mid = 0;
    double med = 0.0;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
            int temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
          }
        }
    }
    if (n % 2 == 0) {
        mid = n / 2;
        med = (double)(arr[mid - 1] + arr[mid]) / 2;
    } else {
        mid = n / 2;
        med = (double)arr[mid];
    }
    std::cout << "The median the array is " << med << std::endl;

}

void mode(int arr[], int n) {
    int maxCount = 0;
    int maxValue = 0;
    for (int i = 0; i < n; i++) {
        int count = 0;
        for (int j = 0; j < n; j++) {
            if (arr[i] == arr[j]) {
            count++;
        }
    }
    if (count > maxCount) {
        maxCount = count;
        maxValue = arr[i];
    }
  }
  std::cout << "The mode of the array is " << maxValue << ", which appears " << maxCount << " times." << std::endl;
}

void Movie_Statistics(void){
    std::cout << "Current function: " << __func__ << std::endl;

    int num_students;
    int *tempnum;
    int temp,i,j;
    char quit;
    std::cout << "input a num" << std::endl;
    std::cin >> num_students;
    int *num = new int[num_students];
    for(i=0;i<num_students;i++){
        std::cin >> temp;
        num[i] = temp;
    }

    average(num,num_students);
    mode(num,num_students);
    median(num,num_students);



}
void func_5(void){
    std::cout << "太简单不想写了" << std::endl;
}
void func_6(void){
    std::cout << "太简单不想写了" << std::endl;
}
void func_7(void){
    std::cout << "太简单不想写了" << std::endl;
}
void func_8(void){
    std::cout << "太简单不想写了" << std::endl;
}

void func_ctrl_week02(void){
    std::cout << "Current function: " << __func__ << std::endl;
    int cmd;
    while(1){
        std::cin>>cmd;
        std::cout << "CMD =" << cmd << std::endl;
        switch(cmd){
            case 1:
                int_sorting();
                break;
            case 2:
                Arr_Convertion_2D();
                break;
            case 3:
                FindMax_Ptr();
                break;
            case 4:
                Movie_Statistics();
                break;
            case 5:
                func_5();
                break;  
            case 6:
                func_6();
                break;  
            case 7:
                func_7();
                break;  
            case 8:
                func_8();
                break;  

            case 0:
                std::cout << "cmd =" <<cmd<<", return."<< std::endl;
                return;
        }
        std::cout << "Current function: " << __func__ << std::endl;
    }
}